package controller;

import com.github.pagehelper.PageInfo;

import domain.Admin;
import domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import service.AdminService;
import service.RoleService;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/admin")
@Secured({"ROLE_SYSTEM"})
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    //查询所有
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize,Admin admin) {
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Admin> allAdmin = adminService.findAllAdmin(pageNum, pageSize,admin);
        modelAndView.getModel().put("page", allAdmin);
        modelAndView.setViewName("admin_all");
        return modelAndView;
    }

    //新增用户
    @RequestMapping("/add")
    public String add(Admin admin, HttpServletRequest request, int[] role) {
        adminService.addAdmin(admin, role);
        return "redirect:" + request.getContextPath() + "/admin/all";
    }

    //用户详情
    @RequestMapping("/desc")
    public ModelAndView desc(int id,String as) {
        ModelAndView modelAndView = new ModelAndView();
        Admin byId = adminService.findById(id);
        modelAndView.getModel().put("admin", byId);
        if (as.equals("desc")){
            modelAndView.setViewName("admin_desc");
        }else {
            modelAndView.setViewName("admin_changePassword");
        }
        return modelAndView;
    }

    //修改用户状态
    @RequestMapping("/updateStatus")
    public String updataStatus(boolean status, @RequestHeader String referer, int[] id) {
        adminService.updataAdminStatus(status, id);
        return "redirect:" + referer;
    }

    //修改用户信息
    @RequestMapping("/update")
    public String update(Admin admin, int[] role) {
        adminService.updateAdmin(admin, role);
        return "redirect:/admin/all";
    }

    //修改前获取信息
    @RequestMapping("/updateInformation")
    public ModelAndView updateInformation(int id) {
        List<Integer> roleIds = new ArrayList();
        ModelAndView modelAndView = new ModelAndView();
        Admin byId = adminService.findById(id);
        List<Role> allRole = roleService.findAllRole();
        modelAndView.getModel().put("admin", byId);
        for (Role role:byId.getRoles()){
            roleIds.add(role.getId());
        }
        modelAndView.getModel().put("roleIds",roleIds);
        modelAndView.getModel().put("roles", allRole);
        modelAndView.setViewName("admin_update");
        return modelAndView;
    }

    //获取所有role
    @RequestMapping("/getAllRoles")
    public ModelAndView getAllRoles(){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> allRole = roleService.findAllRole();
        modelAndView.setViewName("admin_add");
        modelAndView.getModel().put("allRole",allRole);
        return modelAndView;
    }

    //修改密码
    @Secured({"ROLE_SYSTEM"})
    @RequestMapping("/changePassword")
    public String changePassword(Admin admin){
        adminService.updateAdminPassword(admin);
        return "redirect:/admin/all";
    }


}
